package com.selfdiscipline

import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject

/**
 * 应用程序类
 * 自律应用的入口点，配置了依赖注入
 */
@HiltAndroidApp
class SelfDisciplineApp : Application(), Configuration.Provider {
    
    @Inject
    lateinit var workerFactory: HiltWorkerFactory
    
    override fun onCreate() {
        super.onCreate()
        
        // Create notification channels
        createNotificationChannels()
    }
    
    private fun createNotificationChannels() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Timer notification channel
            val timerChannel = NotificationChannel(
                CHANNEL_TIMER_ID,
                getString(R.string.channel_timer_name),
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = getString(R.string.channel_timer_description)
                setShowBadge(true)
            }
            
            // Task reminder notification channel
            val taskReminderChannel = NotificationChannel(
                CHANNEL_TASK_REMINDER_ID,
                getString(R.string.channel_task_reminder_name),
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = getString(R.string.channel_task_reminder_description)
                setShowBadge(true)
            }
            
            // Habit tracking notification channel
            val habitChannel = NotificationChannel(
                CHANNEL_HABIT_ID,
                getString(R.string.channel_habit_name),
                NotificationManager.IMPORTANCE_DEFAULT
            ).apply {
                description = getString(R.string.channel_habit_description)
                setShowBadge(true)
            }
            
            // Focus mode notification channel
            val focusModeChannel = NotificationChannel(
                CHANNEL_FOCUS_MODE_ID,
                getString(R.string.channel_focus_mode_name),
                NotificationManager.IMPORTANCE_HIGH
            ).apply {
                description = getString(R.string.channel_focus_mode_description)
                setShowBadge(true)
            }
            
            // Register all channels
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannels(
                listOf(timerChannel, taskReminderChannel, habitChannel, focusModeChannel)
            )
        }
    }
    
    // WorkManager configuration with Hilt integration
    override fun getWorkManagerConfiguration(): Configuration {
        return Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .setMinimumLoggingLevel(android.util.Log.INFO)
            .build()
    }
    
    companion object {
        const val CHANNEL_TIMER_ID = "timer_channel"
        const val CHANNEL_TASK_REMINDER_ID = "task_reminder_channel"
        const val CHANNEL_HABIT_ID = "habit_channel"
        const val CHANNEL_FOCUS_MODE_ID = "focus_mode_channel"
        
        // Notification IDs
        const val NOTIFICATION_TIMER_ID = 1001
        const val NOTIFICATION_TASK_REMINDER_ID = 1002
        const val NOTIFICATION_HABIT_ID = 1003
        const val NOTIFICATION_FOCUS_MODE_ID = 1004
    }
} 